class Category < ActiveRecord::Base
  belongs_to  :parent, foreign_key: :father , class_name: :Category
  has_many    :children, foreign_key: :father , class_name: :Category , dependent: :destroy

  scope :root, ->{ self.where(:father => 0) }

  def news_nav
  	if self
  		cate = Category.where(id: self.father).first
  	  str1 = "<span class='n_gray'>&gt;</span><a href='/news/?category_id=#{cate.id}' class='n_gray'>#{cate.cataname}</a>" if cate.present?
  	  str2 = "<span class='n_gray'>&gt;</span><span class='n_gray'>#{self.cataname}</span>"
  	end
  	"#{str1}#{str2}"
  end

  def self.cate_id name
    Category.where(:cataname => name).first.id
  end

end
